/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.FhInfoFloor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.FhInfoFloorRepository;
import me.zhengjie.service.FhInfoFloorService;
import me.zhengjie.service.dto.FhInfoFloorDto;
import me.zhengjie.service.dto.FhInfoFloorQueryCriteria;
import me.zhengjie.service.mapstruct.FhInfoFloorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author admin
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class FhInfoFloorServiceImpl implements FhInfoFloorService {

    private final FhInfoFloorRepository fhInfoFloorRepository;
    private final FhInfoFloorMapper fhInfoFloorMapper;

    @Override
    public Map<String,Object> queryAll(FhInfoFloorQueryCriteria criteria, Pageable pageable){
        Page<FhInfoFloor> page = fhInfoFloorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(fhInfoFloorMapper::toDto));
    }

    @Override
    public List<FhInfoFloorDto> queryAll(FhInfoFloorQueryCriteria criteria){
        return fhInfoFloorMapper.toDto(fhInfoFloorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FhInfoFloorDto findById(Integer id) {
        FhInfoFloor fhInfoFloor = fhInfoFloorRepository.findById(id).orElseGet(FhInfoFloor::new);
        ValidationUtil.isNull(fhInfoFloor.getId(),"FhInfoFloor","id",id);
        return fhInfoFloorMapper.toDto(fhInfoFloor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FhInfoFloorDto create(FhInfoFloor resources) {
        if (fhInfoFloorRepository.findByName(resources.getName()) != null) {
            throw new BadRequestException(resources.getName()+"数据已存在");
        }
        return fhInfoFloorMapper.toDto(fhInfoFloorRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FhInfoFloor resources) {
        FhInfoFloor fhInfoFloor = fhInfoFloorRepository.findById(resources.getId()).orElseGet(FhInfoFloor::new);
        ValidationUtil.isNull( fhInfoFloor.getId(),"FhInfoFloor","id",resources.getId());
        if (fhInfoFloorRepository.findByNameAndIdIsNot(resources.getName(),resources.getId()) != null) {
            throw new BadRequestException(resources.getName()+"数据已存在");
        }
        fhInfoFloor.copy(resources);
        fhInfoFloorRepository.save(fhInfoFloor);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            fhInfoFloorRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FhInfoFloorDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FhInfoFloorDto fhInfoFloor : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("栋数", fhInfoFloor.getRidgepoleCount());
            map.put("楼层数", fhInfoFloor.getLevelCount());
            map.put("建筑面积", fhInfoFloor.getArea());
            map.put("车间数", fhInfoFloor.getWorkshopCount());
            map.put("企业数", fhInfoFloor.getCompanyCount());
            map.put("楼名称", fhInfoFloor.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}